/**
 * 
 */
package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextAttribute;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

import com.alee.extended.image.WebDecoratedImage;
import com.alee.extended.painter.BorderPainter;
import com.alee.laf.StyleConstants;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.scroll.WebScrollPane;

import controller.Controls;

import model.Recipe;
import model.RecipeIngredient;
import model.RecipeStep;

/**
 * Panel displays recipes in a list format. Used to display search results, 
 * favorites, and recently viewed recipes.
 * 
 * @author
 */
public class RecipeListPanel extends JPanel {
    /**
     * Serial Version ID.
     */
    private static final long serialVersionUID = 8447145240280172772L;
    private static final Dimension ONE_RECIPE_SIZE = new Dimension(400, 100);
    /**
     * 
     */
    private static final String PREP = "<html>Prep Time<Br> ";
    /**
     * 
     */
    private static final String COOK = "<html>Cook Time<Br> ";
    /**
     * 
     */
    private static final String LEVEL = "<html>Level<Br> ";
    /**
     * 
     */
    private final List<Recipe> my_recipes;
    /**
     * 
     */
    private final String my_title;
    /**
     * 
     */
    private Controls my_controller;
    /**
     * 
     * @param the_recipeList
     */
    public RecipeListPanel(Controls the_controller, List<Recipe> the_recipeList, String the_title) {
        my_recipes = the_recipeList;
        my_title = the_title;
        my_controller = the_controller;
        this.setOpaque(false);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setupGrid();
    }
    //TEST
    public RecipeListPanel(List<Recipe> the_recipeList, String the_title) {
        my_recipes = the_recipeList;
        my_title = the_title;
        this.setOpaque(false);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setupGrid();
    }
    //TEST
    public RecipeListPanel(Controls the_controller, String the_title) {
        my_controller = the_controller;
        my_recipes = setupTestRecipeList();
        my_title = the_title;
        this.setOpaque(false);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setupGrid();
    }
    private void setupGrid() {
        JPanel gridPanel = new JPanel();
        gridPanel.setOpaque(false);
        GridLayout grid = new GridLayout(my_recipes.size(), 1);
        //grid.setHgap(5);
        grid.setVgap(10);
        gridPanel.setLayout(grid);
        
        for (int i = 0; i < my_recipes.size(); i++) {
            //Individual recipe display
            JPanel outerPanel = new JPanel();
            //WebPanel outerPanel = new WebPanel();
            //outerPanel.setRound (StyleConstants.largeRound);
            //outerPanel.setUndecorated (false);
            
            outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.X_AXIS));
            JPanel innerGrid = new JPanel();

            GridLayout inner = new GridLayout(2, 1);
            innerGrid.setLayout(inner);
            //Make the title a clickable button.
            JPanel titleButton = createActiveTitle(i + 1, my_recipes.get(i));
            
            JPanel timePanel = new JPanel();
            timePanel.setLayout(new BoxLayout(timePanel, BoxLayout.X_AXIS));
            JLabel prep = new JLabel(PREP + my_recipes.get(i).getPrepTime() + " min</html>", JLabel.CENTER);
          
            JLabel cook = new JLabel(COOK + my_recipes.get(i).getCookTime() + " min</html>", JLabel.CENTER);
            cook.setOpaque(false);
            JLabel level = new JLabel(LEVEL + my_recipes.get(i).getDifficulty() + "</html>", JLabel.CENTER);

            timePanel.add(prep);
            timePanel.add(cook);
            timePanel.add(level);
            innerGrid.add(titleButton);
            innerGrid.add(timePanel);
            outerPanel.add(innerGrid);     
            if (my_recipes.get(i).getPicture() != null && !my_recipes.get(i).getPicture().isEmpty()) {
                BackgroundPanel pic = new BackgroundPanel(BackgroundPanel.createBackgroundImage(this, my_recipes.get(i).getPicture()), 0); 
                pic.setBorder(BorderFactory.createEtchedBorder());
                pic.setPreferredSize(new Dimension(125, 100));
                pic.setMaximumSize(new Dimension(200, 200));
                outerPanel.add(pic);
            }    
            outerPanel.setMaximumSize(ONE_RECIPE_SIZE);
            outerPanel.setPreferredSize(ONE_RECIPE_SIZE);
            outerPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED), 
                                                              BorderFactory.createBevelBorder(BevelBorder.LOWERED)));
            prep.setBorder(BorderFactory.createDashedBorder(null));
            cook.setBorder(BorderFactory.createDashedBorder(null));
            level.setBorder(BorderFactory.createDashedBorder(null));
            //opacity
            level.setOpaque(false);
            prep.setOpaque(false);
            timePanel.setOpaque(false);
            //titleLabel.setOpaque(false);
            innerGrid.setOpaque(false);
            outerPanel.setOpaque(false);
            JPanel outerHolder = new JPanel();
            outerHolder.add(outerPanel);
            outerHolder.setOpaque(false);
            gridPanel.add(outerHolder);
        }
        gridPanel.setMaximumSize(new Dimension(460, 400));
        gridPanel.setBorder(BorderFactory.createEmptyBorder(0,20,20,20));
        JLabel listTitle = new JLabel();
        listTitle.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
        listTitle.setText(my_title);
        listTitle.setAlignmentX(CENTER_ALIGNMENT);
        listTitle.setBorder(BorderFactory.createEmptyBorder(25, 0, 0, 0));
        listTitle.setOpaque(false);
        //JScrollPane scroll = new JScrollPane(gridPanel);
        WebScrollPane scroll = new WebScrollPane(gridPanel);
        scroll.getViewport().setOpaque(false);
        scroll.setOpaque(false);
        scroll.getWebUI().setDrawBorder(false);
        scroll.setMaximumSize(new Dimension(500, 1000));
        this.add(listTitle);
        this.add(scroll);      
    }
    private JPanel createActiveTitle(final int the_number, final Recipe the_recipe) {
        JLabel titleButton = new JLabel(" " + the_number + ". " + the_recipe.getTitle(), JLabel.LEFT);
        titleButton.setForeground(Color.BLUE);
        final Font originalfont = titleButton.getFont();      
        titleButton.addMouseListener(new MouseListener() {
               @Override
               public void mouseClicked(MouseEvent arg0) {
                   my_controller.displayRecipe(the_recipe);
               }
               @SuppressWarnings({ "rawtypes", "unchecked" })
               @Override
               public void mouseEntered(MouseEvent arg0) {
                   Map attributes = originalfont.getAttributes();
                   attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                   arg0.getComponent().setFont(originalfont.deriveFont(attributes));  
               }
               @Override
               public void mouseExited(MouseEvent arg0) {
                   arg0.getComponent().setFont(originalfont);
               }
               @Override
               public void mousePressed(MouseEvent arg0) {
                   // Do nothing   
               }
               @Override
               public void mouseReleased(MouseEvent arg0) {
                   // Do nothing   
               }
           });
        JPanel buttonHolder = new JPanel();
        buttonHolder.setLayout(new BoxLayout(buttonHolder, BoxLayout.X_AXIS));
        buttonHolder.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        buttonHolder.setOpaque(false);
        buttonHolder.add(titleButton);
        return buttonHolder;
    }
    //Helper to test RecipeListPanel.
    private List<Recipe> setupTestRecipeList() {
        List<RecipeIngredient> tacoIngredients = new ArrayList<RecipeIngredient>();
        List<RecipeStep> tacoSteps = new ArrayList<RecipeStep>();
        tacoIngredients.add(new RecipeIngredient(3.0, "whole", "", "ancho chiles"));
        tacoIngredients.add(new RecipeIngredient(4.0, "unpeeled", "", "cloves garlic"));
        tacoIngredients.add(new RecipeIngredient(3.0, "extra-virgin", "tablespoons", "olive oil"));
        tacoIngredients.add(new RecipeIngredient(2.0, "", "tablespoons", "honey"));
        tacoIngredients.add(new RecipeIngredient(2.0, "dried", "teaspoons", "oregano"));
        tacoIngredients.add(new RecipeIngredient(4.0, "boneless", "pounds", "pork shoulder"));
        tacoSteps.add(new RecipeStep(1, "Put the ancho and pasilla chiles and the garlic in a bowl; add 2 to 3 tablespoons water. Microwave on high until soft and pliable, 2 to 3 minutes. Stem and seed the chiles; peel the garlic. Transfer the chiles and garlic to a blender."));
        tacoSteps.add(new RecipeStep(2, "Add the chipotles, onion, 2 tablespoons olive oil, honey, vinegar, 1 tablespoon salt and the oregano to the blender; puree until smooth. Heat the remaining 1 tablespoon oil in a large skillet over high heat; add the chile sauce and fry, stirring, until thick and fragrant, about 8 minutes. Pour in the broth and reduce until slightly thickened."));
        tacoSteps.add(new RecipeStep(3, "Season the pork all over with salt and pepper and transfer to a large slow cooker. Add the bay leaves and cinnamon stick, then pour in the sauce. Cover and cook on high until the meat is tender, about 5 hours."));
        tacoSteps.add(new RecipeStep(4, "Discard the bay leaves and cinnamon stick. Shred the pork with 2 forks; season with salt and pepper. Serve the shredded pork in the tortillas, along with toppings."));
        
        Recipe test1 = new Recipe("Shredded Pork Tacos", "Matt Adams", 20, 30, 6, "Easy", "Backgrounds/2_roman_chicken.jpg", 
                                  "Amazing!", tacoIngredients, tacoSteps);
        Recipe test2 = new Recipe("Pork Tacos with sour cream", 14, 34, "Easy", null);
        Recipe test3 = new Recipe("Pork Tacos with hot sauce", 10, 30, "Intermediate", "");
        Recipe test4 = new Recipe("Pork Tacos with a dash of magic", 20, 30, "Hard", "");
        List<Recipe> test = new ArrayList<Recipe>();
        test.add(test1);
        test.add(test2);
        test.add(test3);
        test.add(test4);
        return test;
    }
    public static void main(String args[]) {
        Recipe test1 = new Recipe("Pork Tacos", 20, 30, "Easy", "Backgrounds/2_roman_chicken.jpg");
        Recipe test2 = new Recipe("Pork Tacos with sour cream", 14, 34, "Easy", "");
        Recipe test3 = new Recipe("Pork Tacos with hot sauce", 10, 30, "Intermediate", "");
        Recipe test4 = new Recipe("Pork Tacos with a dash of magic", 20, 30, "Hard", "");
        List<Recipe> test = new ArrayList<Recipe>();
        test.add(test1);
        test.add(test2);
        test.add(test3);
        test.add(test4);
        JPanel listPanel = new RecipeListPanel(test, "Recently Viewed");
        JPanel background = new JPanel();
        background.add(listPanel);
        background.setBackground(Color.WHITE);
        JFrame frame = new JFrame();
        frame.add(background);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);      
        
    }
}
